package com.wudi.estate.service.impl.assetmanage;

import com.wudi.estate.bean.assetmanage.Asset;
import com.wudi.estate.bean.assetmanage.Device;
import com.wudi.estate.bean.communitymanagement.Community;
import com.wudi.estate.mapper.assetmanage.AssetMapper;
import com.wudi.estate.mapper.assetmanage.DeviceMapper;
import com.wudi.estate.mapper.communitymanagement.CommunityMapper;
import com.wudi.estate.service.assetmanage.AssetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AssetServiceImpl implements AssetService {
    @Autowired
    private AssetMapper assetMapper;
    @Autowired
    private CommunityMapper communityMapper;
    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public List<Map<String, Object>> selectDeviceAndCommunity() throws Exception {
        List<Asset> assets = assetMapper.selectList(null);
        List<Map<String, Object>> lists = new ArrayList<>();
        for (Asset asset : assets) {
            Map<String,Object> maps =new HashMap<>();
            maps.put("assetById",asset);
            Community communityById = communityMapper.selectById(asset.getCid());
            maps.put("communityById",communityById);
            Device deviceById = deviceMapper.selectById(asset.getDid());
            maps.put("deviceById",deviceById);
            lists.add(maps);
        }
        return lists;
    }

    @Override
    public Map<String, Object> selectDeviceAndCommunityAddUi() throws Exception {
        Map<String,Object> maps = new HashMap<>();
        List<Community> communities = communityMapper.selectList(null);
        List<Device> devices = deviceMapper.selectList(null);
        maps.put("communities",communities);
        maps.put("devices",devices);
        return maps;
    }

    @Override
    public void insert(Asset asset) throws Exception {
        assetMapper.insert(asset);
    }

    @Override
    public Asset selectById(long id) {
        return assetMapper.selectById(id);
    }

    @Override
    public void update(Asset asset) {
        System.out.println(asset+"asfewfcsa");
         assetMapper.updateById(asset);
    }

    @Override
    public void delete(long id) {
        Asset asset = assetMapper.selectById(id);
        asset.setState(!asset.isState());
        assetMapper.updateById(asset);
    }
}
